Troubleshooting Tips

Sometimes you get results that you don't expect. Most of the time the cause of the error is related to the user not understanding exactly what the syntax is/should be. But there is the occasional insiduous issue related to using the wrong version, library files being mixed up or simply that you think a particular state (such as username) is 'xyz' when in fact it is 'abc.' Here are some checks to make sure that you cover your bases.

Output the Python version being used

In [1]:
import sys
print("Python version: ", sys.version)
import os
print("Python is installed in : ", os.path.dirname(sys.executable))
Python version:  3.5.0 (default, Feb  5 2017, 17:58:04) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)]
Python is installed in :  /Users/gonzalobriceno/Files/Programming/venvp3/bin

For an imported module output the version and file location

In [2]:
import pandas
print("Pandas version: ", pandas.__version__)
import inspect
print("Pandas is installed in : ", inspect.getfile(pandas))
Pandas version:  0.23.3
Pandas is installed in :  /Users/gonzalobriceno/Files/Programming/venvp3/lib/python3.5/site-packages/pandas/__init__.py

Output the environment variables

You should get the pretty print (pprint) module for this. Note that you can get:

  • environment variables
  • user this script is running as PWD
  • your shell
In [3]:
import pprint 
pprint.pprint(dict(os.environ), width=1)
{'Apple_PubSub_Socket_Render': '/private/tmp/com.apple.launchd.UGcmKBKY5C/Render',
 'CLICOLOR': '1',
 'GIT_PAGER': 'cat',
 'HOME': '/Users/gonzalobriceno',
 'JPY_PARENT_PID': '5866',
 'LANG': 'en_US.utf-8',
 'LC_ALL': 'en_US.utf-8',
 'LOGNAME': 'gonzalobriceno',
 'LSCOLORS': 'ExFxCxDxBxegedabagacad',
 'MPLBACKEND': 'module://ipykernel.pylab.backend_inline',
 'OLDPWD': '/Users/gonzalobriceno/Files/Programming/venvp3/bin',
 'PAGER': 'cat',
 'PATH': '/Users/gonzalobriceno/Files/Programming/venvp3/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin:/Applications/Wireshark.app/Contents/MacOS:/usr/local/mysql/bin',
 'PS1': '(venvp3) '
        '\\h:\\W '
        '\\u\\$ ',
 'PWD': '/Users/gonzalobriceno/Files/Programming/venvp3',
 'PYTHONPATH': '/Users/gonzalobriceno/Files/Programming/venvp3/lib/python3.5/site-packages:/Users/gonzalobriceno/Files/Programming/workspace-eclipse-oxygen/ghb.python',
 'SHELL': '/bin/bash',
 'SHLVL': '1',
 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.i6gsOvoknu/Listeners',
 'TERM': 'xterm-color',
 'TERM_PROGRAM': 'Apple_Terminal',
 'TERM_PROGRAM_VERSION': '400',
 'TERM_SESSION_ID': 'AD5B631B-64A9-491F-A20C-E0C6C7834883',
 'TMPDIR': '/var/folders/2d/1pxbp8md5917hjzz6ygxnqtm0000gn/T/',
 'USER': 'gonzalobriceno',
 'VIRTUAL_ENV': '/Users/gonzalobriceno/Files/Programming/venvp3',
 'XPC_FLAGS': '0x0',
 'XPC_SERVICE_NAME': '0',
 '_': '/Users/gonzalobriceno/Files/Programming/venvp3/bin/ipython',
 '__CF_USER_TEXT_ENCODING': '0x1F5:0x0:0x0',
 '__PYVENV_LAUNCHER__': '/Users/gonzalobriceno/Files/Programming/venvp3/bin/python3'}

For a particular module output the available functions

In case that you don't have code completion enabled

In [4]:
data = {'CBOE/VIX': 'VIX Close', 'FRED/TEDRATE': ''}
pprint.pprint(dir(data), width=1) 
print(type(data))
print("")

if data.__class__ == 'str'.__class__:
    print("varialbe is a '{0}' and has value = {1}".format(x.__class__, x))
elif data.__class__ == [1, 2, "list"].__class__: 
    for item in data:
        print("Item has value = {}".format(item))
else:
    for key, values in data.items():
        print("key has type = '{0}' and value = '{1}'".format(type(key), key))
        print("values has type = '{0}' and value = '{1}'".format(type(values), values))
        print("")
['__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'clear',
 'copy',
 'fromkeys',
 'get',
 'items',
 'keys',
 'pop',
 'popitem',
 'setdefault',
 'update',
 'values']
<class 'dict'>

key has type = '<class 'str'>' and value = 'FRED/TEDRATE'
values has type = '<class 'str'>' and value = ''

key has type = '<class 'str'>' and value = 'CBOE/VIX'
values has type = '<class 'str'>' and value = 'VIX Close'

Does a particular file exist?

In [5]:
import os.path
PATH='/Users/gonzalobriceno/Files/music.txt'

if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
    print("File exists and is readable")
else:
    print("Either file is missing or is not readable")
File exists and is readable

Did a particular command fail or succeed?

Remember that a return vaue of 0 means the call was successful

In [6]:
command = 'cp /Users/gonzalobriceno/Files/music.txt /Users/gonzalobriceno/Files/music2.txt'
retvalue = os.system(command)
print('Command {0} returned {1}.'.format(command,retvalue))
Command cp /Users/gonzalobriceno/Files/music.txt /Users/gonzalobriceno/Files/music2.txt returned 0.